package com.android.stockme.frontend;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import com.android.stockme.backend.IBackendController;
import com.android.stockme.backend.fetcher.INewsItem;
import com.android.stockme.backend.news.INewsUpdateReceivedListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter implements ListAdapter {

	private IBackendController backend = null;
	private Context context = null;
	private StockMeNewsTab newsTab = null;
	
	private LayoutInflater inflater = null;

	private ArrayList<INewsItem> newsUpdates = null;
	
	public NewsAdapter(final IBackendController backend, Context context, StockMeNewsTab newsTab) {
		this.backend = backend;
		this.context = context;
		this.newsTab = newsTab;

		this.newsUpdates = new ArrayList<INewsItem>(this.backend.getNewsUpdatesCenter().fetchNewsSortedByDate());
		
		this.backend.getNewsUpdatesCenter().registerNewsUpdateReceivedListener(new INewsUpdateReceivedListener() {
			public void notifyNewsUpdateReceived(Object arg) {
				newsUpdates = new ArrayList<INewsItem>(backend.getNewsUpdatesCenter().fetchNewsSortedByDate());
				notifyDataSetChanged();
			}			
		});
		
		// DEBUG!!!
//		Collection<INewsItem> debug_items = new HashSet<INewsItem>();
//		debug_items.add(new INewsItem() {
//			public String getLink() {
//				return "http://www.teva.com";
//			}
//
//			public String getPublishDate() {
//				return "2009-05-10 19:22:15";
//			}
//
//			public String getSymbol() {
//				return "TEVA";
//			}
//
//			public String getTitle() {
//				return "Teva sells generic medicine and it is a very profitable bussiness. They are fucking loaded!!!";
//			}
//
//			public int compareTo(INewsItem another) {
//				return 0;
//			}
//
//			public INewsItem clone() throws CloneNotSupportedException {
//				return this;
//			}
//
//			public void setLink(String link) {
//			}
//
//			public void setPublishDate(String pubdate) {
//			}
//
//			public void setSymbol(String symbol) {
//			}
//
//			public void setTitle(String title) {
//			}
//		});
//		this.backend.getNewsUpdatesCenter().updateNewsCenter(debug_items);
		// DEBUG!!!
	}
	
	public int getCount() {
		return this.newsUpdates.size();
	}

	public Object getItem(int position) {
		return this.newsUpdates.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		inflater = LayoutInflater.from(context);
		RelativeLayout cellLayout = (RelativeLayout)inflater.inflate(R.layout.news_node2, null);
		cellLayout.setMinimumWidth(parent.getWidth());
		int color;
		if(position % 2 > 0)
			color = Color.rgb(48,92,131);
		else 
			color = Color.rgb(119,138,170);
		cellLayout.setBackgroundColor(color);

		// The last item is the most up-to-date so we need to reverse the order
		INewsItem news = this.newsUpdates.get((this.newsUpdates.size() - 1) - position);
		
		TextView field = (TextView)cellLayout.findViewById(R.id.news_symbol);
		field.setBackgroundColor(color);
		field.setText(news.getSymbol());
		field.setClickable(true);
		field.setOnClickListener(this.newsTab);

		field = (TextView)cellLayout.findViewById(R.id.news_pub_date);
		field.setText(news.getPublishDate());
		field.setClickable(true);
		field.setOnClickListener(this.newsTab);

		field = (TextView)cellLayout.findViewById(R.id.news_title);
		field.setText(news.getTitle());
		field.setClickable(true);
		field.setOnClickListener(this.newsTab);

		field = (TextView)cellLayout.findViewById(R.id.news_link);
		field.setText(news.getLink());
		field.setClickable(true);
		field.setOnClickListener(this.newsTab);

		cellLayout.setId(position);
		cellLayout.setClickable(true);
		cellLayout.setOnClickListener(this.newsTab);
		return cellLayout;
	}
	
	public void setSymbolFilter(String symbol) {
		
		if (symbol == null || symbol.compareTo("") == 0) {
			this.newsUpdates = new ArrayList<INewsItem>(this.backend.getNewsUpdatesCenter().fetchNewsSortedByDate());
			this.notifyDataSetChanged();		
			return;
		}
		
		ArrayList<INewsItem> updates = new ArrayList<INewsItem>(this.backend.getNewsUpdatesCenter().fetchNewsSortedByFilter(symbol));
		
		if (updates.size() == 0) {
			AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.context);
			dialogBuilder.setCancelable(false);
			dialogBuilder.setTitle("Empty Results");
			dialogBuilder.setPositiveButton("OK", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			dialogBuilder.setMessage("Filtering request returned no results");
			dialogBuilder.create().show();
			return;
		}
		
		this.newsUpdates = updates;
		this.notifyDataSetChanged();		
	}
}
